'''
===============
limber.ui.menus
===============

This module implements the menus' related objects,
such as *MenuBar* and *Menu*.
'''

from PyQt4 import QtCore, QtGui


class MenuBar(QtGui.QMenuBar):
    
    def __init__(self, parent=None):
        super(MenuBar, self).__init__(parent)
        
        self._menus = {}
        self._actions = {}
    
    def addMenu(self, arg1, arg2=None):
        '''Adds a menu to the MenuBar.'''
        
        if arg2 is not None:
            result = super(MenuBar, self).addMenu(arg1, arg2)
            is_menu = True
        else:
            result = super(MenuBar, self).addMenu(arg1)
            is_menu = not isinstance(result, QtGui.QAction)
        
        if is_menu:
            menu = result
        else:
            menu = arg1
        
        name = unicode(menu.title())
        
        self._menus[name] = menu
        return result
    
    def insertMenu(self, action, menu):
        '''Inserts the given *menu* before *action*.'''
        
        result = super(MenuBar, self).insertMenu(action, menu)
        
        name = unicode(menu.title())
        self._menus[name] = menu
        return result
    
    def addAction(self, action, *args, **kwargs):
        '''Adds an action to the MenuBar.'''
        
        if not args and not kwargs:
            super(MenuBar, self).addAction(action)
            
            name = unicode(action.text())
            self._actions[name] = action
            return None
        
        result = super(MenuBar, self).addAction(action, *args, **kwargs)
        
        name = unicode(result.text())
        self._actions[name] = action
        return result
    
    def clear(self):
        '''Clears the MenuBar.'''
        
        super(MenuBar, self).clear()
        
        self._menus = {}
        self._actions = {}


class Menu(QtGui.QMenu):
    
    def __init__(self, arg1=None, arg2=None):
        if arg2 is not None and arg1 is None:
            raise TypeError('The first parameter must be provided.')
        if arg2 is None:
            super(Menu, self).__init__(arg1)
        else:
            super(Menu, self).__init__(arg1, arg2)
        
        self._actions = {}
        self._menus = {}
    
    def addMenu(self, arg1, arg2=None):
        '''Adds a menu to the Menu.'''
        
        if arg2 is not None:
            result = super(Menu, self).addMenu(arg1, arg2)
            is_menu = True
        else:
            result = super(Menu, self).addMenu(arg1)
            is_menu = not isinstance(result, QtGui.QAction)
        
        if is_menu:
            menu = result
        else:
            menu = arg1
        
        name = unicode(menu.title())
        
        self._menus[name] = menu
        return result
    
    def insertMenu(self, action, menu):
        '''Inserts the given *menu* before *action*.'''
        
        result = super(Menu, self).insertMenu(action, menu)
        
        name = unicode(menu.title())
        self._menus[name] = menu
        return result
    
    def addAction(self, action, *args, **kwargs):
        '''Adds an action to the Menu.'''
        
        if not args and not kwargs:
            super(Menu, self).addAction(action)
            
            name = unicode(action.text())
            self._actions[name] = action
            return None
        
        result = super(Menu, self).addAction(action, *args, **kwargs)
        
        name = unicode(result.text())
        self._actions[name] = action
        return result
    
    def clear(self):
        '''Clears the Menu.'''
        
        super(Menu, self).clear()
        
        self._menus = {}
        self._actions = {}